﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;

using PasswordProvider.ViewModel.Properties;
using PasswordProvider.ObjectModel;
using PasswordProvider.ViewModel.Framework;
using PasswordProvider.ViewModel.Service;

namespace PasswordProvider.ViewModel
{
    public class MainWindowViewModel : VisualViewModel
    {
        #region Construction

        public MainWindowViewModel()
            : base(Strings.MainWindow_Title)
        {
            NavigationManager.Navigated += new EventHandler(NavigationManager_Navigated);
            Settings.Default.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Settings_PropertyChanged);         
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Whether or not to keep window on top when running as normal window.
        /// </summary>
        public bool AlwaysOnTop
        {
            get { return _alwaysOnTop; }
            set { SetPropertyValue(value, ref _alwaysOnTop, "AlwaysOnTop"); }
        } private bool _alwaysOnTop = Settings.Default.AlwaysOnTop; 
        
        /// <summary>
        /// Whether or not to minimize to the notification area when running as normal window
        /// </summary>
        public bool MinimizeToSystemTray
        {
            get { return _minimizeToSystemTray; }
            set { SetPropertyValue(value, ref _minimizeToSystemTray, "MinimizeToSystemTray"); }
        } private bool _minimizeToSystemTray = Settings.Default.MinimizeToSystemTray;
        
        /// <summary>
        /// Whether or not to run in the notification area.
        /// </summary>
        public bool RunInSystemTray
        {
            get { return _runInSystemTray; }
            set { SetPropertyValue(value, ref _runInSystemTray, "RunInSystemTray"); }
        } private bool _runInSystemTray = Settings.Default.RunInSystemTray;
        
        /// <summary>
        /// Whether or not to pin the window open when mode is run in notification area.
        /// </summary>
        public bool PinWindowOpen
        {
            get { return _pinWindowOpen; }
            set { SetPropertyValue(value, ref _pinWindowOpen, "PinWindowOpen"); }
        } private bool _pinWindowOpen = Settings.Default.PinWindowOpen; 
        
        /// <summary>
        /// Visibility of the main menu.
        /// </summary>
        public Visibility MenuVisibility
        {
            get { return _menuVisibility; }
            private set 
            { 
               base.SetPropertyValue(value, ref _menuVisibility, "MenuVisibility"); 
            }
        } Visibility _menuVisibility = Visibility.Collapsed;

        /// <summary>
        /// Window Startup Location. (Set via xaml)
        /// </summary>
        public static Rect WindowPosition
        {
            get
            {
                return Settings.Default.WindowStartupLocation;
            }
            set
            {
                Settings.Default.WindowStartupLocation = value;
                Settings.Default.Save();
            }
        }        

        /// <summary>
        /// Command to show the file actions page.
        /// </summary>
        public CommandViewModel ShowManageCommand
        {
            get
            {
                if (_showManageCommand == null)
                {
                    _showManageCommand = new CommandViewModel(
                        Strings.MainWindow_ShowManageCommand_DisplayName,
                        Strings.MainWindow_ShowManageCommand_ToolTip,
                        new RelayCommand(param=>ShowManageCommandExecuted()));
                }
                return _showManageCommand;
            }
        } CommandViewModel _showManageCommand;
        
        /// <summary>
        /// Command to show the file actions page.
        /// </summary>
        public CommandViewModel ShowOptionsCommand
        {
            get
            {
                if (_showOptionsCommand == null)
                {
                    _showOptionsCommand = new CommandViewModel(
                        Strings.MainWindow_ShowOptionsPageCommand_DisplayName,
                        Strings.MainWindow_ShowOptionsPageCommand_ToolTip,
                        new RelayCommand(param => ShowOptionsCommandExecuted()));
                }
                return _showOptionsCommand;
            }
        } CommandViewModel _showOptionsCommand;
                
        /// <summary>
        /// Command to log out
        /// </summary>
        public CommandViewModel ShowHelpCommand
        {
            get
            {
                if (_showHelpCommand == null)
                {
                    _showHelpCommand = new CommandViewModel(
                        Strings.MainWindow_ShowHelpCommand_DisplayName,
                        Strings.MainWindow_ShowHelpCommand_ToolTip,
                        new RelayCommand(param => ShowHelpCommandExecuted(param)));
                }
                return _showHelpCommand;
            }
        } CommandViewModel _showHelpCommand;
        
        /// <summary>
        /// Command to log out
        /// </summary>
        public CommandViewModel LogOffCommand
        {
            get
            {
                if (_logOffCommand == null)
                {
                    _logOffCommand = new CommandViewModel(
                        Strings.MainWindow_LogOffCommand_DisplayName,
                        Strings.MainWindow_LogOffCommand_ToolTip,
                        new RelayCommand(param => LogOffCommandExecuted()));
                }
                return _logOffCommand;
            }
        } CommandViewModel _logOffCommand;
        
        #endregion // Public Properties

        #region Command Handlers

        /// <summary>
        /// Show help command executed.
        /// </summary>
        private void ShowHelpCommandExecuted(object param)
        {
            string section = null; 
            if (param == null)
            {
                object current = NavigationManager.CurrentView;
                if (current != null)
                {
                    SequentialPageViewerViewModel pageViewer = current as SequentialPageViewerViewModel;
                    if (pageViewer != null)
                    {
                        section = pageViewer.CurrentView.GetType().Name;
                    }
                    else
                    {
                        section = current.GetType().Name;
                    }

                    section = section.Replace("ViewModel", "");
                    section = section.Replace("LoginSite", "Site");
                    section = section.Replace("CreditCard", "Site");
                }
            }

            MessagingService.RequestShowHelp(this, section);
        }

        /// <summary>
        /// Logout command executed.
        /// </summary>
        private static void LogOffCommandExecuted()
        {
            PasswordProviderData.Current.SecurityManager.InvalidateAuthentication();
            NavigationManager.GoToEnterPasswordView();
        }

        /// <summary>
        /// Show file actions page command executed.
        /// </summary>
        private static void ShowManageCommandExecuted()
        {
            NavigationManager.GoToManageView();
        }
        /// <summary>
        /// Show options page command executed.
        /// </summary>
        private static void ShowOptionsCommandExecuted()
        {
            NavigationManager.GoToOptionsView();
        }
        
        

        #endregion // Command Handlers

        #region Private Helpers

        void NavigationManager_Navigated(object sender, EventArgs e)
        {
            SetMenuVisibility();
        }

        void Settings_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "AlwaysOnTop":
                    AlwaysOnTop = Settings.Default.AlwaysOnTop;
                    break;
                case "MinimizeToSystemTray":
                    MinimizeToSystemTray = Settings.Default.MinimizeToSystemTray;
                    break;
                case "RunInSystemTray":
                    RunInSystemTray = Settings.Default.RunInSystemTray;                    
                    break;
                case "PinWindowOpen":
                    PinWindowOpen = Settings.Default.PinWindowOpen;
                    break;
            }
        }

        private void SetMenuVisibility()
        {
            // Show, Hide the main meun depending on whether the current page is the list view.
            if (RunInSystemTray || !(NavigationManager.CurrentView is SitesListViewModel))
                MenuVisibility = Visibility.Collapsed;
            else
                MenuVisibility = Visibility.Visible;
        }

        #endregion

    }
}
